const mongoose = require('mongoose')
mongoose.set('useFindAndModify', false);
const Schemas = require('../models/index')
const request = require('request')

const Player = mongoose.model('Player', Schemas.Player)
const PlayerAPI = {
  getOpenid: code => {
    return new Promise((resolve) => {
      request.get({
        url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxa44ae21611cfa7de&secret=ae6eaa71c5bdf92a8b500797a1c9c058&js_code=${code}&grant_type=authorization_code`,
      }, function (error, response, body) {
        if (response.statusCode == 200) {
          var playerinfo = JSON.parse(body);
          console.log('获取微信信息成功！', playerinfo);
          resolve({
            code: 200,
            message: '获取openid成功!',
            data: {
              openid: playerinfo.openid,
              session_key: playerinfo.session_key
            }
          });
        } else {
          console.log(response.statusCode);
          resolve({
            code: response.statusCode,
            message: '获取openid失败！',
            data: body
          })
        }
      });
    })
  },
  getRank: openid => {
    return new Promise((resolve) => {
      Player.find().exec(function (err, docs) {
        if (err) {
          resolve({
            code: 500,
            message: err
          })
        } else {
          let myScore = null
          docs.sort(function (a, b) {
            if (a.openid == openid) {
              myScore = a
            }
            if (b.openid == openid) {
              myScore = b
            }
            return b.bestScore - a.bestScore
          })
          resolve({
            code: 200,
            message: 'success',
            data: {
              rank: docs,
              myScore: myScore
            }
          })
        }
      })
    })
  },
  updateMyScore: data => {
    return new Promise((resolve) => {
      Player.find({ openid: data.openid }).exec(function (err, docs) {
        console.log(docs)
        if (err) {
          resolve({
            code: 500,
            message: err
          })
        } else {
          if(docs.length > 0 ) {
            Player.findOneAndUpdate({ openid: data.openid }, data, function(err1, docs1) {
              if(err1) {
                resolve({
                  code: 500,
                  message: err1
                })
              } else {
                resolve({
                  code: 200,
                  message: '更新成绩成功！',
                  data: docs1
                })
              }
            })
          } else {
            let player = new Article(data)
            player.save(function(err2, docs2) {
              if (err2) {
                resolve({
                  code: 500,
                  message: err2
                })
              } else {
                resolve({
                  code: 200,
                  message: '创建成绩成功！',
                  data: docs2
                })
              }
            })
          }
        }
      })
    })
  },
}

module.exports = PlayerAPI